package com.skivingcloud.admin.sys.service.impl;

import com.mybatisflex.core.BaseMapper;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.row.Db;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.skivingcloud.admin.constants.Constant;
import com.skivingcloud.admin.sys.entity.SysRole;
import com.skivingcloud.admin.sys.entity.SysRoleMenu;
import com.skivingcloud.admin.sys.mapper.SysRoleMenuMapper;
import com.skivingcloud.admin.sys.service.SysRoleMenuService;
import com.skivingcloud.common.utils.UuidUtil;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 *  服务层实现。
 *
 * @author skivingcloud
 * @since 2023-08-28
 */
@Service
public class SysRoleMenuServiceImpl extends ServiceImpl<SysRoleMenuMapper, SysRoleMenu> implements SysRoleMenuService {
    
    /**
     * 设置角色资源权限
     *
     * @param role 角色资源
     */
    @Override
    public void setResourceByRole(SysRole role){
        QueryWrapper sm = QueryWrapper.create();
        sm.where(SysRoleMenu::getRoleId).eq(role.getId());
        this.mapper.deleteByQuery(sm);
        List<SysRoleMenu> list = Arrays.stream(role.getResId()).map(r -> {
            SysRoleMenu sysRoleMenu = new SysRoleMenu();
            sysRoleMenu.setId(UuidUtil.get32UUID());
            sysRoleMenu.setRoleId(role.getId());
            sysRoleMenu.setMenuId(r);
            return sysRoleMenu;
        }).toList();
        //批量插入
        Db.executeBatch(list, Constant.BATCH_SIZE, SysRoleMenuMapper.class, BaseMapper::insert);
    }
    
    /**
     * 加载角色的菜单
     *
     * @param roleId 角色id
     * @return 菜单列表
     */
    @Override
    public Map<String, Object> getAllMenuForRole(String roleId){
        return null;
    }
}
